package com.soft.javase.ch06.date;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.*;
/**
 * @author wangzhisen
 * @Desc TODO
 * @date 2025/4/25
 */
// 新闻类
class News {
    String title;
    String content;
    LocalDateTime time;

    public News(String title, String content, LocalDateTime time) {
        this.title = title;
        this.content = content;
        this.time = time;
    }

    @Override
    public String toString() {
        return "标题: " + title + ", 内容: " + content + ", 时间: " + time;
    }
}

// 学生类
class Student {
    String name;
    String studentId;
    LocalDate birthDate;

    public Student(String name, String studentId, LocalDate birthDate) {
        this.name = name;
        this.studentId = studentId;
        this.birthDate = birthDate;
    }

    @Override
    public String toString() {
        return "姓名: " + name + ", 学号: " + studentId + ", 出生日期: " + birthDate;
    }
}

public class DateCalculation2 {
    public static void main(String[] args) {
        // 1. 计算出生日期至今的天数
        LocalDate birthDate = LocalDate.of(2000, 1, 1);
        LocalDate today = LocalDate.now();
        long daysSinceBirth = ChronoUnit.DAYS.between(birthDate, today);
        System.out.println("出生日期至今的天数: " + daysSinceBirth);

        // 2. 当前日期三天后的日期
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime threeDaysLater = now.plusDays(3);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
        System.out.println("当前日期三天后的日期: " + threeDaysLater.format(formatter));

        // 3. 求食品过期时间
        LocalDate productionDate = LocalDate.of(2024, 5, 10);
        LocalDate expirationDate = productionDate.plusDays(180);
        System.out.println("食品过期时间: " + expirationDate);

        // 4. 求上个月的第三天是星期几
        LocalDate lastMonthThirdDay = today.minusMonths(1).withDayOfMonth(3);
        DayOfWeek dayOfWeek = lastMonthThirdDay.getDayOfWeek();
        System.out.println("上个月的第三天是: " + dayOfWeek);

        // 5. 找出下个月的倒数第3天是哪天
        LocalDate nextMonthLastThirdDay = today.plusMonths(1).withDayOfMonth(today.plusMonths(1).lengthOfMonth() - 2);
        System.out.println("下个月的倒数第3天是: " + nextMonthLastThirdDay);

        // 6. 请问今天是今年的第多少天
        int dayOfYear = today.getDayOfYear();
        System.out.println("今天是今年的第 " + dayOfYear + " 天");

        // 7. 输入任意一个年份，得到这年的2月份有几天
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入一个年份: ");
        int year = scanner.nextInt();
        YearMonth yearMonth = YearMonth.of(year, 2);
        int daysInFebruary = yearMonth.lengthOfMonth();
        System.out.println(year + " 年的2月份有 " + daysInFebruary + " 天");

        // 8. 新闻类按时间降序排列
        List<News> newsList = new ArrayList<>();
        newsList.add(new News("新闻1", "内容1", LocalDateTime.of(2024, 10, 1, 10, 0)));
        newsList.add(new News("新闻2", "内容2", LocalDateTime.of(2024, 10, 2, 11, 0)));
        newsList.add(new News("新闻3", "内容3", LocalDateTime.of(2024, 10, 3, 12, 0)));
        newsList.add(new News("新闻4", "内容4", LocalDateTime.of(2024, 10, 4, 13, 0)));
        newsList.add(new News("新闻5", "内容5", LocalDateTime.of(2024, 10, 5, 14, 0)));
        newsList.add(new News("新闻6", "内容6", LocalDateTime.of(2024, 10, 6, 15, 0)));
        newsList.add(new News("新闻7", "内容7", LocalDateTime.of(2024, 10, 7, 16, 0)));
        newsList.add(new News("新闻8", "内容8", LocalDateTime.of(2024, 10, 8, 17, 0)));
        newsList.add(new News("新闻9", "内容9", LocalDateTime.of(2024, 10, 9, 18, 0)));
        newsList.add(new News("新闻10", "内容10", LocalDateTime.of(2024, 10, 10, 19, 0)));

        newsList.sort((n1, n2) -> n2.time.compareTo(n1.time));
        System.out.println("按时间降序排列的新闻:");
        for (News news : newsList) {
            System.out.println(news);
        }

        // 9. 找出比“张三”大的所有同学信息
        List<Student> studentList = new ArrayList<>();
        studentList.add(new Student("张三", "001", LocalDate.of(2000, 1, 1)));
        studentList.add(new Student("李四", "002", LocalDate.of(1999, 1, 1)));
        studentList.add(new Student("王五", "003", LocalDate.of(2001, 1, 1)));

        LocalDate zhangsanBirthDate = null;
        for (Student student : studentList) {
            if ("张三".equals(student.name)) {
                zhangsanBirthDate = student.birthDate;
                break;
            }
        }

        if (zhangsanBirthDate != null) {
            System.out.println("比“张三”大的同学信息:");
            for (Student student : studentList) {
                if (student.birthDate.isBefore(zhangsanBirthDate)) {
                    System.out.println(student);
                }
            }
        }
    }
}
